package com.core.controller;

import com.common.enums.ErrorCode;
import com.common.response.BaseResponse;
import com.common.response.ResultUtils;
import com.core.service.SysUserService;
import com.common.utils.JwtUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class LogOutController {
    @Resource
    private SysUserService sysUserService;
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public BaseResponse userLogout(HttpServletRequest request) {
        String token = request.getHeader(JwtUtils.TOKEN_HEADER);
        String userName = JwtUtils.getUserNameByToken(token);
        boolean logoutFlag = sysUserService.userLogout(userName);
        if (logoutFlag) {
            return ResultUtils.success("退出成功！");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR, "退出失败，请联系开发人员");
    }
}
